﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Xml;

namespace YQHL.GS
{
    public class Utility
    {
        /// <summary>
        /// JSON字符串转 XML
        /// </summary>
        public static XmlDocument JsonConvertToXml(string jsonStr)
        {
            return JsonConvert.DeserializeXmlNode(jsonStr);
        }

        /// <summary>
        /// JSON 转 XML
        /// </summary>
        public static XmlDocument JsonConvertToXml(JObject jObject)
        {
            return JsonConvertToXml(jObject.ToString());
        }

        /// <summary>
        /// XML 转 JObject
        /// </summary>
        public static JObject XmlConvertJson(XmlDocument xml)
        {
            var jsonStr = JsonConvert.SerializeXmlNode(xml);
            return JObject.Parse(jsonStr);
        }

        /// <summary>
        /// XML字符串 转 JObject
        /// </summary>
        public static JObject XmlConvertJson(string xmlStr)
        {
            var xml = new XmlDocument();
            xml.LoadXml(xmlStr);
            return XmlConvertJson(xml);
        }
    }
}